#region  == Copyright == 
// =====================================================================
//  Microsoft Consulting Services France - Aymeric Mouillé - 2013
//  Projet     : CRM Solution Management Tool
//  Fichier    : SolutionHelper.cs (30/04/2013 18:04)
//  
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// =====================================================================
#endregion

namespace CRM.Solution.Management.Tool
{
    #region  == Using == 
    using System;
    using System.Xml;

    #endregion

    /// <summary>
    /// </summary>
    public class SolutionHelper
    {
        #region Get Import Log Status
        /// <summary>
        /// Gets the Import Log Status.
        /// </summary>
        /// <param name="dataXml">The data XML.</param>
        /// <returns></returns>
        public static string GetImportLogStatus(string dataXml)
        {
            string status = string.Empty;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(dataXml);

            try
            {
                var node = xmlDoc.SelectSingleNode("importexportxml");
                if (node != null)
                {
                    if (node.Attributes != null
                        && node.Attributes["status"] != null)
                    {
                        status = node.Attributes["status"].Value;
                    }
                }

                if (string.IsNullOrEmpty(status))
                {
                    node = xmlDoc.SelectSingleNode("importexportxml/solutionManifests/solutionManifest/result");
                    if (node != null)
                    {
                        if (node.Attributes != null
                            && node.Attributes["result"] != null)
                        {
                            status = node.Attributes["result"].Value;
                            if (!string.IsNullOrEmpty(node.Attributes["errortext"].Value))
                            {
                                status += string.Format("  ({0})", node.Attributes["errortext"].Value);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // ignore
            }
            return status;
        }
        #endregion
    }
}
